package com.cindata

import grails.converters.JSON
import grails.gorm.transactions.Transactional
import org.springframework.security.access.annotation.Secured

import static org.springframework.http.HttpStatus.*

@Transactional
@Secured(['isAuthenticated()'])
class MenuController
{
    def springSecurityService

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    def index(Integer max)
    {
        params.max = Math.min(max ?: 10, 100)
        params.offset = params.offset ? params.offset.toInteger() : 0
        max = params.max
        def sort = params['sort']
        def order = params['order']
        def title = params['title']
        def offset = params.offset
        def sql = "  from Menu m where 1=1 "
        if (title)
        {
            sql += " and title like '%${title}%' "
        }
        def sql1 = " select count(*) " + sql
        if (order && sort)
        {
            sql += " order by m.${sort} ${order}"
        }
        else
        {
            sql += "order by m.orderNum asc "
        }
        def menuList = Menu.executeQuery(sql, [max: max, offset: offset]).asList()
        def count = Menu.executeQuery(sql1)
        respond menuList, model: [menuList: menuList, menuCount: count[0]]
    }

    /**
     * 根据id查询二级菜单列表*/
    def querySubListMenuByPId(Menu menu)
    {
        def sql = " from Menu m where m.parent = " + menu.id
        def menuSubList = Menu.executeQuery(sql).asList()

        render([status: "success", menuSubList: menuSubList] as JSON)
        //        respond menu,model: [menuSubList:menuSubList]

    }

    def show(Menu menu)
    {
        respond menu
    }

    def create()
    {
        respond new Menu(params)
    }

    @Transactional
    def save(Menu menu)
    {
        if (menu == null)
        {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (menu.hasErrors())
        {
            transactionStatus.setRollbackOnly()
            respond menu.errors, view: 'create'
            return
        }

        menu.save flush: true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.created.message', args: [message(code: 'menu.label', default: 'AccountCity'), menu.id])
                redirect menu
            }
            '*' { respond menu, [status: CREATED] }
        }
    }

    def edit(Menu menu)
    {
        respond menu
    }

    @Transactional
    def update(Menu menu)
    {
        println menu
        if (menu == null)
        {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (menu.hasErrors())
        {
            transactionStatus.setRollbackOnly()
            respond menu.errors, view: 'edit'
            return
        }

        menu.save flush: true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.updated.message', args: [message(code: 'menu.label', default: 'Menu'), menu.id])
                redirect menu
            }
            '*' { respond menu, [status: OK] }
        }
    }

    @Transactional
    def delete(Menu menu)
    {
        println menu

        if (menu == null)
        {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        menu.delete flush: true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.deleted.message', args: [message(code: 'menu.label', default: 'Menu'), menu.id])
                redirect action: "index", method: "GET"
            }
            '*' { render status: NO_CONTENT }
        }
    }

    protected void notFound()
    {
        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.not.found.message', args: [message(code: 'menu.label', default: 'Menu'), params.id])
                redirect action: "index", method: "GET"
            }
            '*' { render status: NOT_FOUND }
        }
    }
}
